# -*- coding: utf-8 -*-

def search(data, key): # 用二分查找搜索目标数据在哪快内
    length = len(data)
    first = 0
    last = length - 1
    print(f"长度:{length}分块的数据是:{data}")
    while first <= last:
        mid = (last + first) // 2
        if data[mid] > key:
            last = mid - 1
        elif data[mid] < key:
            first = mid + 1
        else:
            return mid
    return False

# 分块查找
def block(data, count, key): # data是列表，count是每块的长度，key是查找的目标数据
    length = len(data)
    # 一共分几块
    block_length = length // count
    if count * block_length != length:
        block_length += 1
    print("一共分", block_length, "块")
    print("分块情况如下：")
    for block_i in range(block_length):
        block_data = []
        for i in range(count):
            if block_i * count + i >= length:
                break
            block_data.append(data[block_i * count + i])
        result = search(block_data, key) # 块内二分查找
        if result != False:
            return block_i * count + result
    return False

data = [23,43,56,78,97,100,120,135,147,150,155]
result = block(data, 4, 150)
print("查找的值的索引位置是：", result)












